#/bin/python2

import numpy as np
import math as m

pi = np.pi

def diffCoefPlane ( k, phi, phi_p, beta = pi/2, n = 2.0, plane='E'):
	
	factor = 0
	if plane is 'E':
		factor =  1.0
	else:
		factor = -1.0
	
	A = -np.exp(-1j*pi/4.0)*np.sin(pi/n)/( 2*n*np.sqrt( 2*pi*k )*np.sin(beta) )
	
	B =  np.cos( pi/n ) - np.cos( ( 1.0*phi - phi_p )/n )
	C =  np.cos( pi/n ) - np.cos( ( 1.0*phi + phi_p )/n )

	D = 1.0/B + factor/C
	
	
	return A*D
	
def EdiffPlane ( E0, s, k, phi, phi_p, beta=pi/2, n=2.0, plane='E' ):
	"""
	@brief Ejecuta la ecuacion 4.35 de Introduction to the UGTD
	@param E0: el campo electrico en el punto de difraccion
	        k: el numero de onda
	        s: la distancia del pto de difraccion al pto de recepcion
	"""
	
	Ei = E0
	D  = diffCoefPlane( k, phi, phi_p, beta, n, plane )
	
	R  = 1.0/np.sqrt( s ) * np.exp( -1j*k*s )
	
	return Ei*D*R
